using System;
using UnityEngine;

namespace PhysSound
{
	[Serializable]
	public class PhysSoundAudioContainer
	{
		public int KeyIndex;

		public AudioSource SlideAudio;

		private PhysSoundMaterial _mat;

		private float _targetVolume;

		private float _baseVol;

		private float _basePitch;

		private float _basePitchRand;

		public PhysSoundAudioContainer(int k)
		{
			KeyIndex = k;
		}

		public void Initialize(PhysSoundMaterial m)
		{
			if (!(SlideAudio == null))
			{
				_mat = m;
				SlideAudio.clip = _mat.GetAudioSet(KeyIndex).Slide;
				_baseVol = SlideAudio.volume;
				_basePitch = SlideAudio.pitch;
				_basePitchRand = _basePitch;
				SlideAudio.loop = true;
				SlideAudio.volume = 0f;
			}
		}

		public void SetTargetVolumeAndPitch(GameObject parentObject, GameObject otherObject, Vector3 relativeVelocity, Vector3 normal, bool exit, float mod = 1f)
		{
			if (!(SlideAudio == null))
			{
				if (!SlideAudio.isPlaying)
				{
					_basePitchRand = _basePitch * _mat.GetScaleModPitch(parentObject.transform.localScale) + _mat.GetRandomPitch();
					SlideAudio.Play();
				}
				SlideAudio.pitch = _basePitchRand + relativeVelocity.magnitude * _mat.SlidePitchMod;
				_targetVolume = ((!exit && _mat.CollideWith(otherObject)) ? (_mat.GetSlideVolume(relativeVelocity, normal) * _baseVol * mod) : 0f);
			}
		}

		public void UpdateVolume()
		{
			if (!(SlideAudio == null))
			{
				SlideAudio.volume = Mathf.MoveTowards(SlideAudio.volume, _targetVolume, 0.06f);
				if (SlideAudio.volume < 0.01f)
				{
					SlideAudio.Stop();
				}
			}
		}

		public bool CompareKeyIndex(int k)
		{
			return k == KeyIndex;
		}

		public void Disable()
		{
			if ((bool)SlideAudio)
			{
				SlideAudio.Stop();
				SlideAudio.enabled = false;
			}
		}

		public void Enable()
		{
			if ((bool)SlideAudio)
			{
				SlideAudio.enabled = true;
			}
		}
	}
}
